package com.lovo.psc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.csc.dto.PurchaseTenderDto;
import com.lovo.psc.Uto.BookBean;
import com.lovo.psc.entity.Purchase;
import com.lovo.psc.entity.SupplierPurchase;
import com.lovo.psc.entity.UserBean;
import com.lovo.psc.mapper.IVieBookMapper;
import com.lovo.psc.service.ISupplierPurchaseService;
import com.lovo.psc.service.IUserService;
import com.lovo.psc.service.IVieBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-08
 */
@Service
public class VieBookServiceImpl extends ServiceImpl<IVieBookMapper, Purchase> implements IVieBookService {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private ISupplierPurchaseService iSupplierPurchaseService;

    @Override
    @RabbitListener(queues = "tenderQueue")
    public void monitorBook(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, PurchaseTenderDto purchaseTenderDto) {
        Purchase purchase = new Purchase();
        purchase.setNumber(purchaseTenderDto.getPurchaseCode() + "");
        purchase.setCommodityname(purchaseTenderDto.getGoodName());
        purchase.setCommoditysiei(purchaseTenderDto.getGoodInfo());
        purchase.setCommoditynum(purchaseTenderDto.getPurchaseNum());
        purchase.setCommoditysort(purchaseTenderDto.getGoodSort());
        try {
            channel.basicAck(tag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据采购书中的经销商类型查询出本地所有的供应商
        List<UserBean> userBeanList = iUserService.findSupplierStateBy(purchase.getCommoditysort());
        //将采购书按供应商id添加到本地数据库
        for (UserBean userBean : userBeanList) {
            purchase.setFkuserId(userBean.getUserpkId());
            super.save(purchase);
        }
    }

    public Purchase findPurchaseById(Integer id) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("PK_purchaseId", id);
        return super.getOne(qw);
    }
    //供应商编号 供应商id
    public List<BookBean> findListBookBean(String number,String id) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("fkuser_id",id);
        List<Purchase> listPurchase = super.list(qw);
        List<SupplierPurchase> listSupplierPurchase = iSupplierPurchaseService.findListSupplierPurchase(number);
        ArrayList<BookBean> arrayList = new ArrayList<BookBean>();

        for (int i = 0; i < listPurchase.size(); i++) {
            Purchase purchase = listPurchase.get(i);
            if (i < listSupplierPurchase.size()) {
                SupplierPurchase supplierPurchase = listSupplierPurchase.get(i);

                if (purchase.getPkPurchaseid().equals(supplierPurchase.getFkmarkbookid())) {
                    BookBean bookBean = new BookBean(purchase.getPkPurchaseid(), number,purchase.getFkuserId(),
                            purchase.getNumber(), purchase.getCommodityname(), purchase.getCommoditysort(),
                            purchase.getCommoditynum(), purchase.getCommoditysiei(), supplierPurchase.getFkmarkbookid(),
                            supplierPurchase.getQuote(), supplierPurchase.getDetails(), supplierPurchase.getState());
                    arrayList.add(bookBean);
                }
            } else {
                BookBean bookBean = new BookBean(purchase.getPkPurchaseid(), number,purchase.getFkuserId(),
                        purchase.getNumber(), purchase.getCommodityname(), purchase.getCommoditysort(),
                        purchase.getCommoditynum(), purchase.getCommoditysiei()
                );
                arrayList.add(bookBean);

            }
        }
//        System.out.println(arrayList);
//        System.out.println(number +"----------"+id);
        return arrayList;
    }
}
